package com.asa.chart.suanming;

import java.awt.EventQueue;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import com.asa.chart.base.Line;
import com.asa.chart.base.Point;
import com.asa.suanming.Block9;



/**
 * 封装mvc
 * @author Administrator
 *
 */
public class AlgoVisualizer {

	private AlgoFrame frame;
	

	
	
	public static Calendar c;
	private long startTime;
	public static long midTime;

	private List<Block9> block9list;
	
	
	
	public AlgoVisualizer(List<Block9> block9list) {
		super();
		final int sceneWidth = 800;
		final int sceneHeight = 600;
		this.block9list = block9list;
		
		init(sceneWidth, sceneHeight);
		c = Calendar.getInstance();
		startTime = c.getTimeInMillis();
		
		
		EventQueue.invokeLater(new Runnable() {//用java事件分发（开了个线程）的方法来创建对话框
			
			public void run() {
				// TODO Auto-generated method stub
				frame = new AlgoFrame("wellcome", sceneWidth, sceneHeight);
				frame.addKeyListener(new AlgoKeyListener());
				frame.addMouseListener(new AlgoMouseListener());
				new Thread(new Runnable() {
					
					public void run() {
						// TODO Auto-generated method stub
						runmiage();
					}

					
				}).start(); ;
			
				
			}
		});
	}

	
	
	
	protected AlgoVisualizer(final int sceneWidth,final int sceneHeight) {
		super();
		

		
		init(sceneWidth, sceneHeight);
		c = Calendar.getInstance();
		startTime = c.getTimeInMillis();
		
		
		EventQueue.invokeLater(new Runnable() {//用java事件分发（开了个线程）的方法来创建对话框
			
			public void run() {
				// TODO Auto-generated method stub
				frame = new AlgoFrame("wellcome", sceneWidth, sceneHeight);
				frame.addKeyListener(new AlgoKeyListener());
				frame.addMouseListener(new AlgoMouseListener());
				new Thread(new Runnable() {
					
					public void run() {
						// TODO Auto-generated method stub
						runmiage();
					}

					
				}).start(); ;
			
				
			}
		});

		
		
	}

	List<Line> mapline;
	List<BlockString> wenzi;
	int blocksize = 200;

	private void init(final int sceneWidth, final int sceneHeight) {

		mapline = new ArrayList<Line>();
		//画辅助线
		for (int i = 0; i <= sceneHeight/100; i++) {
			Point spoint = new Point();
			Point epoint = new Point();

			spoint.x = 0;
			spoint.y = i*blocksize;
			epoint.x = sceneWidth-blocksize;
			epoint.y = i*blocksize;
			Line line = new Line();
			line.spoint = spoint;
			line.epoint = epoint;
			mapline.add(line);
			
			
		}
		for (int i = 0; i < sceneWidth/100; i++) {
			
			Point spoint = new Point();
			Point epoint = new Point();

			spoint.x = i*blocksize;
			spoint.y = 0;
			epoint.x = i*blocksize;
			epoint.y = sceneHeight;
			Line line = new Line();
			line.spoint = spoint;
			line.epoint = epoint;
			mapline.add(line);
			
			
			
		}
		
		
		wenzi = new ArrayList<BlockString>();
		
		int index = 1;
		//天干
		for (int i = 0; i < sceneHeight/blocksize; i++) {
			
			for (int j = 0; j < sceneWidth/blocksize - 1; j++) {
				BlockString blockString = new BlockString();
				blockString.x = j*blocksize + blocksize/5*4;
				blockString.y = i*blocksize + blocksize/5*4;
				
				
				if (index<block9list.size()) {
					blockString.content = block9list.get(index).getTiangan();

				}else {
					blockString.content = i+"  "+j;
				}
				index++;
				wenzi.add(blockString);
			}
		}
		//幻方
		List<BlockString> numberlist = new ArrayList<BlockString>();
		
		for (int i = 0; i < wenzi.size(); i++) {
			BlockString blockString = wenzi.get(i);
			BlockString blockStringnew = new BlockString();
			blockStringnew.x = blockString.x+10;
			blockStringnew.y = blockString.y+10;
			numberlist.add(blockStringnew);
		}
		numberlist.get(0).content = "4";
		numberlist.get(1).content = "9";
		numberlist.get(2).content = "2";
		numberlist.get(3).content = "3";
		numberlist.get(4).content = "5";
		numberlist.get(5).content = "7";
		numberlist.get(6).content = "8";
		numberlist.get(7).content = "1";
		numberlist.get(8).content = "6";

		//八卦
		List<BlockString> bagualist = new ArrayList<BlockString>();

		for (int i = 0; i < wenzi.size(); i++) {
			
			BlockString blockString = wenzi.get(i);
			BlockString blockStringnew = new BlockString();
			blockStringnew.x = blockString.x-20;
			blockStringnew.y = blockString.y-20;
			blockStringnew.content = block9list.get(i).getBagua2shi();
			bagualist.add(blockStringnew);
			
		}

		//方位
		List<BlockString> fangweilist = new ArrayList<BlockString>();

		for (int i = 0; i < wenzi.size(); i++) {
			
			BlockString blockString = wenzi.get(i);
			BlockString blockStringnew = new BlockString();
			blockStringnew.x = blockString.x-20;
			blockStringnew.y = blockString.y-40;
			blockStringnew.content = block9list.get(i).getFangwei();
			fangweilist.add(blockStringnew);
			
		}
		
		//阴阳的划分
		List<BlockString> yinyanglist = new ArrayList<BlockString>();

		for (int i = 0; i < wenzi.size(); i++) {
			
			BlockString blockString = wenzi.get(i);
			BlockString blockStringnew = new BlockString();
			blockStringnew.x = blockString.x-90;
			blockStringnew.y = blockString.y-90;
			blockStringnew.content = block9list.get(i).getYinyang();
			yinyanglist.add(blockStringnew);
			
		}
		
		
		//五行
		List<BlockString> wuxinglist = new ArrayList<BlockString>();

		for (int i = 0; i < wenzi.size(); i++) {
			
			BlockString blockString = wenzi.get(i);
			BlockString blockStringnew = new BlockString();
			blockStringnew.x = blockString.x-140;
			blockStringnew.y = blockString.y-140;
			blockStringnew.content = block9list.get(i).getWuxing();
			wuxinglist.add(blockStringnew);
			
		}
		
		//五季
		List<BlockString> wujilist = new ArrayList<BlockString>();

		for (int i = 0; i < wenzi.size(); i++) {
					
			BlockString blockString = wenzi.get(i);
			BlockString blockStringnew = new BlockString();
			blockStringnew.x = blockString.x-140;
			blockStringnew.y = blockString.y-120;
			blockStringnew.content = block9list.get(i).getWuji();
			wuxinglist.add(blockStringnew);
					
		}
		
		
		//12地支
		List<BlockString> dizhilist = new ArrayList<BlockString>();
		BlockString blockString = new BlockString();
		blockString.x = 10;
		blockString.y = 0 * blocksize + blocksize/2;
		blockString.content = "辰";
		dizhilist.add(blockString);
		blockString = new BlockString();
		blockString.x = 10;
		blockString.y = 1 * blocksize + blocksize/2;
		blockString.content = "卯";
		dizhilist.add(blockString);
		blockString = new BlockString();
		blockString.x = 10;
		blockString.y = 2 * blocksize + blocksize/2;
		blockString.content = "寅";
		dizhilist.add(blockString);

		blockString = new BlockString();
		blockString.x = sceneWidth  - blocksize - 10;
		blockString.y = 0 * blocksize + blocksize/2;
		blockString.content = "申";
		dizhilist.add(blockString);
		blockString = new BlockString();
		blockString.x = sceneWidth - blocksize - 10;
		blockString.y = 1 * blocksize + blocksize/2;
		blockString.content = "酉";
		dizhilist.add(blockString);
		blockString = new BlockString();
		blockString.x = sceneWidth - blocksize - 10;
		blockString.y = 2 * blocksize + blocksize/2;
		blockString.content = "戌";
		dizhilist.add(blockString);
		
		blockString = new BlockString();
		blockString.x = 0 * blocksize + blocksize/2;
		blockString.y = 10;
		blockString.content = "巳";
		dizhilist.add(blockString);
		blockString = new BlockString();
		blockString.x = 1 * blocksize + blocksize/2;
		blockString.y = 10;
		blockString.content = "午";
		dizhilist.add(blockString);
		blockString = new BlockString();
		blockString.x = 2 * blocksize + blocksize/2;
		blockString.y = 10;
		blockString.content = "未";
		dizhilist.add(blockString);
//		长生、沐浴、冠带、临官、帝旺（好的阶段）；衰、病、死、墓、绝（不好阶段）；胎、养（一般阶段）
////		{"子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"}
		blockString = new BlockString();
		blockString.x = 0 * blocksize + blocksize/2;
		blockString.y = sceneHeight - 10;
		blockString.content = "丑";
		dizhilist.add(blockString);
		blockString = new BlockString();
		blockString.x = 1 * blocksize + blocksize/2;
		blockString.y = sceneHeight - 10;
		blockString.content = "子";
		dizhilist.add(blockString);
		blockString = new BlockString();
		blockString.x = 2 * blocksize + blocksize/2;
		blockString.y = sceneHeight - 10;
		blockString.content = "亥";
		dizhilist.add(blockString);
		
		
		
		
		wenzi.addAll(numberlist);
		wenzi.addAll(bagualist);
		wenzi.addAll(fangweilist);
		wenzi.addAll(yinyanglist);
		wenzi.addAll(wuxinglist);
		wenzi.addAll(wujilist);
		wenzi.addAll(dizhilist);

		
	}
	
	private void runmiage() {
		while(true) {
			
			//绘制数据
			frame.render(mapline,wenzi);
			c = Calendar.getInstance();
			long endTime = c.getTimeInMillis();
			midTime = (endTime - startTime) / 1000;
			
			
			
		}
	}
	
	
	boolean isAnimated;
	private class AlgoKeyListener extends KeyAdapter{

	
		@Override
		public void keyReleased(KeyEvent event) {
			// TODO Auto-generated method stub
			super.keyReleased(event);
			if (event.getKeyChar()==' ') {
				isAnimated = !isAnimated;
			}
		
		}
		
	}
	
	private class AlgoMouseListener extends MouseAdapter{
		
		
		@Override
		public void mousePressed(MouseEvent event) {
			// TODO Auto-generated method stub

			  
			event.translatePoint(0, -(frame.getBounds().height-frame.getCanvasHeight()));

			
			
			java.awt.Point point = event.getPoint();
			System.out.println(point);
			int x = point.x;
			int y = point.y;
			
			
			int index = x/blocksize;
			int indey = y/blocksize;
			
			System.out.println(index);
			System.out.println(indey);
			
			
		}

		
		
		
		
		
		
		
		
	}
	
	
	
	
	
	
	
}
